package myApp.controller;

import myApp.controller.user.NewUser;
import myApp.controller.user.ResetUserPassword;
import myApp.model.user.Role;
import myApp.model.user.User;
import myApp.repository.UserConstraintException;
import myApp.service.UserService;
import myApp.util.AppLog;
import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.ViewAccessScoped;
import org.hibernate.validator.constraints.NotEmpty;
import org.omnifaces.util.Messages;
import org.primefaces.context.RequestContext;
import org.slf4j.Logger;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Produces;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

@Named("logs_ctrl")
@RequestScoped
public class LogsController implements Serializable {

    private static final long serialVersionUID = -1L;

// ------------------------------ FIELDS ------------------------------

    @Inject @AppLog private Logger log;

    private String buffer;

    @PostConstruct
    public void init() {

        String logRootDir = System.getProperty("LOG_ROOT_DIR");
        log.debug("Log root dir : {}", logRootDir);

        try {
            FileInputStream fis = new FileInputStream(logRootDir + "/access.log")  ;

            StringBuilder text = new StringBuilder();
            String NL = System.getProperty("line.separator");
            Scanner scanner = new Scanner(fis);
            try {
                while (scanner.hasNextLine()){
                    text.append(scanner.nextLine() + NL);
                }
            }
            finally{
                scanner.close();
            }
            buffer = text.toString();
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public String getBuffer() {
        return buffer;
    }

    public void setBuffer(String buffer) {
        this.buffer = buffer;
    }
}
